home *** CD-ROM | disk | FTP | other *** search
/ The Complete Utilities To…ka 501 Killer Utilities! / 501 Killer Utilities! (Macworld July 1995).cdr / Programming / Glypha III Src / Code / Utilities.c < prev   
Encoding:
Text File  |  1995-01-29  |  10.5 KB  |  425 lines  |  [TEXT/MPCC]

  1.  
  2. //============================================================================
  3. //----------------------------------------------------------------------------
  4. //                                    Utilities.c
  5. //----------------------------------------------------------------------------
  6. //============================================================================
  7.  
  8.  
  9. #include "Externs.h"
  10.  
  11.  
  12. #define kActive                        0
  13. #define kInactive                    255
  14.  
  15.  
  16. GDHandle    thisGDevice;
  17. long        tickNext;
  18.  
  19.  
  20. //==============================================================  Functions
  21. //--------------------------------------------------------------  RandomInt
  22.  
  23. short RandomInt (short range)
  24. {
  25.     register long    rawResult;
  26.     
  27.     rawResult = Random();
  28.     if (rawResult < 0L)
  29.         rawResult *= -1L;
  30.     rawResult = (rawResult * (long)range) / 32768L;
  31.     
  32.     return ((short)rawResult);
  33. }
  34.  
  35. //--------------------------------------------------------------  RedAlert
  36.  
  37. void RedAlert (StringPtr theStr)
  38. {
  39.     #define        kRedAlertID        128
  40.     short        whoCares;
  41.     
  42.     ParamText(theStr, "\p", "\p", "\p");
  43.     whoCares = Alert(kRedAlertID, 0L);
  44.     ExitToShell();
  45. }
  46.  
  47. //--------------------------------------------------------------  FindOurDevice
  48.  
  49. void FindOurDevice (void)
  50. {
  51.     thisGDevice = GetMainDevice();
  52.     if (thisGDevice == 0L)
  53.         RedAlert("\pCouldn't Find Our Device");
  54. }
  55.  
  56. //--------------------------------------------------------------  LoadGraphic
  57.  
  58. void LoadGraphic (short resID)
  59. {
  60.     Rect        bounds;
  61.     PicHandle    thePicture;
  62.     
  63.     thePicture = GetPicture(resID);
  64.     if (thePicture == 0L)
  65.         RedAlert("\pA Graphic Couldn't Be Loaded");
  66.     
  67.     HLock((Handle)thePicture);
  68.     bounds = (*thePicture)->picFrame;
  69.     HUnlock((Handle)thePicture);
  70.     OffsetRect(&bounds, -bounds.left, -bounds.top);
  71.     DrawPicture(thePicture, &bounds);
  72.     
  73.     ReleaseResource((Handle)thePicture);
  74. }
  75.  
  76. //--------------------------------------------------------------  CreateOffScreenPixMap
  77.  
  78. void CreateOffScreenPixMap (Rect *theRect, CGrafPtr *offScreen)
  79. {
  80.     CTabHandle    thisColorTable;
  81.     GDHandle    oldDevice;
  82.     CGrafPtr    newCGrafPtr;
  83.     Ptr            theseBits;
  84.     long        sizeOfOff, offRowBytes;
  85.     OSErr        theErr;
  86.     short        thisDepth;
  87.     
  88.     oldDevice = GetGDevice();
  89.     SetGDevice(thisGDevice);
  90.     newCGrafPtr = 0L;
  91.     newCGrafPtr = (CGrafPtr)NewPtrClear(sizeof(CGrafPort));
  92.     if (newCGrafPtr != 0L)
  93.     {
  94.         OpenCPort(newCGrafPtr);
  95.         thisDepth = (**(*newCGrafPtr).portPixMap).pixelSize;
  96.         offRowBytes = ((((long)thisDepth * 
  97.                 (long)(theRect->right - theRect->left)) + 15L) >> 4L) << 1L;
  98.         sizeOfOff = (long)(theRect->bottom - theRect->top) * offRowBytes;
  99.         OffsetRect(theRect, -theRect->left, -theRect->top);
  100.         theseBits = NewPtr(sizeOfOff);
  101.         if (theseBits != 0L)
  102.         {
  103.             (**(*newCGrafPtr).portPixMap).baseAddr = theseBits;
  104.             (**(*newCGrafPtr).portPixMap).rowBytes = (short)offRowBytes + 0x8000;
  105.             (**(*newCGrafPtr).portPixMap).bounds = *theRect;
  106.             thisColorTable = (**(**thisGDevice).gdPMap).pmTable;
  107.             theErr = HandToHand((Handle *)&thisColorTable);
  108.             (**(*newCGrafPtr).portPixMap).pmTable = thisColorTable;
  109.             ClipRect(theRect);
  110.             RectRgn(newCGrafPtr->visRgn, theRect);
  111.             ForeColor(blackColor);
  112.             BackColor(whiteColor);
  113.             EraseRect(theRect);
  114.         }
  115.         else
  116.         {
  117.             CloseCPort(newCGrafPtr);        
  118.             DisposePtr((Ptr)newCGrafPtr);
  119.             newCGrafPtr = 0L;
  120.             RedAlert("\pCouldn't Allocate Enough Memory");
  121.         }
  122.     }
  123.     else
  124.         RedAlert("\pCouldn't Allocate Enough Memory");
  125.     
  126.     *offScreen = newCGrafPtr;
  127.     SetGDevice(oldDevice);
  128. }
  129.  
  130. //--------------------------------------------------------------  CreateOffScreenBitMap
  131.  
  132. void CreateOffScreenBitMap (Rect *theRect, GrafPtr *offScreen)
  133. {
  134.     GrafPtr        theBWPort;
  135.     BitMap        theBitMap;    
  136.     long        theRowBytes;
  137.     
  138.     theBWPort = (GrafPtr)(NewPtr(sizeof(GrafPort)));
  139.     OpenPort(theBWPort);
  140.     theRowBytes = (long)((theRect->right - theRect->left + 15L) / 16L) * 2L;
  141.     theBitMap.rowBytes = (short)theRowBytes;
  142.     theBitMap.baseAddr = NewPtr((long)theBitMap.rowBytes * 
  143.         (theRect->bottom - theRect->top));
  144.     if (theBitMap.baseAddr == 0L)
  145.         RedAlert("\pCouldn't Create Bitmaps");
  146.     theBitMap.bounds = *theRect;
  147.     if (MemError() != noErr)
  148.         RedAlert("\pCouldn't Create Bitmaps");
  149.     SetPortBits(&theBitMap);
  150.     ClipRect(theRect);
  151.     RectRgn(theBWPort->visRgn, theRect);
  152.     EraseRect(theRect);
  153.     *offScreen = theBWPort;
  154. }
  155.  
  156. //--------------------------------------------------------------  ZeroRectCorner
  157.  
  158. void ZeroRectCorner (Rect *theRect)        // Offset rect to (0, 0)
  159. {
  160.     theRect->right -= theRect->left;
  161.     theRect->bottom -= theRect->top;
  162.     theRect->left = 0;
  163.     theRect->top = 0;
  164. }
  165.  
  166. //--------------------------------------------------------------  FlashShort
  167.  
  168. void FlashShort (short theValue)
  169. {
  170.     GrafPtr            wasPort, tempPort;
  171.     Str255            tempStr;
  172.     Rect            tempRect;
  173.     
  174.     GetPort(&wasPort);
  175.     
  176.     tempPort = (GrafPtr)NewPtrClear(sizeof(GrafPort));
  177.     OpenPort(tempPort);
  178.     
  179.     NumToString((long)theValue, tempStr);
  180.     MoveTo(20,40);
  181.     SetRect(&tempRect, 18, 20, 122, 42);
  182.     EraseRect(&tempRect);
  183.     DrawString(tempStr);
  184.     
  185.     ClosePort(tempPort);
  186.     SetPort((GrafPtr)wasPort);
  187. }
  188.  
  189. //--------------------------------------------------------------  LogNextTick
  190.  
  191. void LogNextTick (long howMany)
  192. {
  193.     tickNext = TickCount() + howMany;
  194. }
  195.  
  196. //--------------------------------------------------------------  WaitForNextTick
  197.  
  198. void WaitForNextTick (void)
  199. {
  200.     do
  201.     {
  202.     }
  203.     while (TickCount() < tickNext);
  204. }
  205.  
  206. //--------------------------------------------------------------  TrapExists  
  207.  
  208. Boolean TrapExists (short trapNumber)
  209. {
  210.     #define        kUnimpTrap        0x9F
  211.     
  212.     return ((NGetTrapAddress(trapNumber, ToolTrap) !=
  213.             NGetTrapAddress(kUnimpTrap, ToolTrap)));
  214. }
  215.  
  216. //--------------------------------------------------------------  DoWeHaveGestalt  
  217.  
  218. Boolean DoWeHaveGestalt (void)
  219. {
  220.     #define        kGestaltTrap    0xAD
  221.     
  222.     return (TrapExists(kGestaltTrap));
  223. }
  224.  
  225. //--------------------------------------------------------------  CenterAlert
  226.  
  227. void CenterAlert (short alertID)
  228. {
  229.     AlertTHndl    alertHandle;
  230.     Rect        theScreen, alertRect;
  231.     short        horiOff, vertOff;
  232.     Byte        wasState;
  233.     
  234.     theScreen = qd.screenBits.bounds;
  235.     theScreen.top += LMGetMBarHeight();
  236.     
  237.     alertHandle = (AlertTHndl)GetResource('ALRT', alertID);
  238.     if (alertHandle != 0L)
  239.     {
  240.         wasState = HGetState((Handle)alertHandle);
  241.         HLock((Handle)alertHandle);
  242.         
  243.         alertRect = (**alertHandle).boundsRect;
  244.         OffsetRect(&alertRect, -alertRect.left, -alertRect.top);
  245.         
  246.         horiOff = ((theScreen.right - theScreen.left) - alertRect.right) / 2;    
  247.         vertOff = ((theScreen.bottom - theScreen.top) - alertRect.bottom) / 3;
  248.         
  249.         OffsetRect(&alertRect, horiOff, vertOff + LMGetMBarHeight());
  250.         
  251.         (**alertHandle).boundsRect = alertRect;
  252.         HSetState((Handle)alertHandle, wasState);
  253.     }
  254. }
  255.  
  256. //--------------------------------------------------------------  RectWide
  257.  
  258. short RectWide (Rect *theRect)
  259. {
  260.     return (theRect->right - theRect->left);
  261. }
  262.  
  263. //--------------------------------------------------------------  RectTall
  264.  
  265. short RectTall (Rect *theRect)
  266. {
  267.     return (theRect->bottom - theRect->top);
  268. }
  269.  
  270. //--------------------------------------------------------------  CenterRectInRect
  271.  
  272. void CenterRectInRect (Rect *rectA, Rect *rectB)
  273. {
  274.     short    widthA, tallA;
  275.     
  276.     widthA = RectWide(rectA);
  277.     tallA = RectTall(rectA);
  278.     
  279.     rectA->left = rectB->left + (RectWide(rectB) - widthA) / 2;
  280.     rectA->right = rectA->left + widthA;
  281.     
  282.     rectA->top = rectB->top + (RectTall(rectB) - tallA) / 2;
  283.     rectA->bottom = rectA->top + tallA;
  284. }
  285.  
  286. //--------------------------------------------------------------  PasStringCopy
  287.  
  288. void PasStringCopy (StringPtr p1, StringPtr p2)
  289. {
  290.     register short        stringLength;
  291.     
  292.     stringLength = *p2++ = *p1++;
  293.     while (--stringLength >= 0)
  294.         *p2++ = *p1++;
  295. }
  296.  
  297. //--------------------------------------------------------------  CenterDialog
  298.  
  299. void CenterDialog (short dialogID)
  300. {
  301.     DialogTHndl    dlogHandle;
  302.     Rect        theScreen, dlogBounds;
  303.     short        hPos, vPos;
  304.     Byte        wasState;
  305.     
  306.     theScreen = qd.screenBits.bounds;
  307.     theScreen.top += LMGetMBarHeight();
  308.     
  309.     dlogHandle = (DialogTHndl)GetResource('DLOG', dialogID);
  310.     if (dlogHandle != 0L)
  311.     {
  312.         wasState = HGetState((Handle)dlogHandle);
  313.         HLock((Handle)dlogHandle);
  314.         
  315.         dlogBounds = (**dlogHandle).boundsRect;
  316.         OffsetRect(&dlogBounds, -dlogBounds.left, -dlogBounds.top);
  317.         
  318.         hPos = ((theScreen.right - theScreen.left) - dlogBounds.right) / 2;
  319.         vPos = ((theScreen.bottom - theScreen.top) - dlogBounds.bottom) / 3;
  320.         
  321.         OffsetRect(&dlogBounds, hPos, vPos + LMGetMBarHeight());
  322.         
  323.         (**dlogHandle).boundsRect = dlogBounds;
  324.         HSetState((Handle)dlogHandle, wasState);
  325.     }
  326. }
  327.  
  328. //--------------------------------------------------------------  DrawDefaultButton
  329.  
  330. void DrawDefaultButton (DialogPtr theDialog)
  331. {
  332.     Rect        itemRect;
  333.     Handle        itemHandle;
  334.     short        itemType;
  335.     
  336.     GetDItem(theDialog, 1, &itemType, &itemHandle, &itemRect);
  337.     InsetRect(&itemRect, -4, -4);
  338.     PenSize(3, 3);
  339.     FrameRoundRect(&itemRect, 16, 16);
  340.     PenNormal();
  341. }
  342.  
  343. //--------------------------------------------------------------  PasStringCopyNum
  344.  
  345. void PasStringCopyNum (StringPtr p1, StringPtr p2, short charsToCopy)
  346. {
  347.     short        i;
  348.     
  349.     if (charsToCopy > *p1)        // if trying to copy more chars than there are
  350.         charsToCopy = *p1;        // reduce the number of chars to copy to this size
  351.     
  352.     *p2 = charsToCopy;
  353.     
  354.     *p2++;
  355.     *p1++;
  356.     
  357.     for (i = 0; i < charsToCopy; i++)
  358.         *p2++ = *p1++;
  359. }
  360.  
  361. //--------------------------------------------------------------  GetDialogString
  362.  
  363. void GetDialogString (DialogPtr theDialog, short item, StringPtr theString)
  364. {
  365.     Rect        itemRect;
  366.     Handle        itemHandle;
  367.     short        itemType;
  368.     
  369.     GetDItem(theDialog, item, &itemType, &itemHandle, &itemRect);
  370.     GetIText(itemHandle, theString);
  371. }
  372.  
  373. //--------------------------------------------------------------  SetDialogString
  374.  
  375. void SetDialogString (DialogPtr theDialog, short item, StringPtr theString)
  376. {
  377.     Rect        itemRect;
  378.     Handle        itemHandle;
  379.     short        itemType;
  380.     
  381.     GetDItem(theDialog, item, &itemType, &itemHandle, &itemRect);
  382.     SetIText(itemHandle, theString);
  383. }
  384.  
  385. //--------------------------------------------------------------  SetDialogNumToStr
  386.  
  387. void SetDialogNumToStr (DialogPtr theDialog, short item, long theNumber)
  388. {
  389.     Str255        theString;
  390.     Rect        itemRect;
  391.     Handle        itemHandle;
  392.     short        itemType;
  393.     
  394.     NumToString(theNumber, theString);
  395.     GetDItem(theDialog, item, &itemType, &itemHandle, &itemRect);
  396.     SetIText(itemHandle, theString);
  397. }
  398.  
  399. //--------------------------------------------------------------  GetDialogNumFromStr
  400.  
  401. void GetDialogNumFromStr (DialogPtr theDialog, short item, long *theNumber)
  402. {
  403.     Str255        theString;
  404.     Rect        itemRect;
  405.     Handle        itemHandle;
  406.     short        itemType;
  407.     
  408.     GetDItem(theDialog, item, &itemType, &itemHandle, &itemRect);
  409.     GetIText(itemHandle, theString);
  410.     StringToNum(theString, theNumber);
  411. }
  412.  
  413. //--------------------------------------------------------------  DisableControl
  414.  
  415. void DisableControl (DialogPtr theDialog, short whichItem)
  416. {
  417.     Rect        iRect;
  418.     Handle        iHandle;
  419.     short        iType;
  420.     
  421.     GetDItem(theDialog, whichItem, &iType, &iHandle, &iRect);
  422.     HiliteControl((ControlHandle)iHandle, kInactive);
  423. }
  424.  
  425.